'use strict';

const uniID = require('uni-id')
const proParams = require('processing-parameters')
exports.main = async (event, context) => {
	let datas = proParams(event)
	let params = ''
	if(datas.data.params){
		params = datas.data.params
	}
	let res = {} 
	let action = datas.data.action
	let token = datas.event.headers.uniidtoken
	switch (action) {
		case 'register':
			res = uniID.register(params);
			break;
		case 'login':
			res = uniID.login(params);
			break;
		case 'checkToken':
			res = uniID.checkToken(token);
			break;
		case 'logout':
			let auth = await uniID.checkToken(token)
			if(auth.code == 0){
				res = await uniID.logout(token)
			}else{
				res = auth
			}
			break;
		case 'sendSmsCode':
			const randomStr = '00000' + Math.floor(Math.random() * 1000000)
			const code = randomStr.substring(randomStr.length - 6)
			res = await uniID.sendSmsCode({
				mobile: params.mobile,
				code,
				type: 'login'
			})
			break;
		case 'loginBySms':
			res = await uniID.loginBySms(params)
			break;
		case 'updateUser':
			let payload = await uniID.checkToken(token)
			res = await uniID.updateUser({uid: payload.uid,...params});
			break;
		default:
			res = {
				code: 403,
				msg: '非法访问'
			}
			break;
	}

	//返回数据给客户端
	return res
	
};
